<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
//           var arr = [
//       {name:'a', age:12},
//       {name:'b', age:13},
//       {name:'c', age:10},
//       {name:'d', age:8},
//       {name:'e', age:22},
//       {name:'f', age:7},
//       {name:'g', age:65},
//       {name:'h', age:33},
//       {name:'i', age:5},
//       {name:'j', age:44},
//       {name:'k', age:6},
//     ];
// // 按年龄
//     // arr.sort((a,b)=>{
//     //     return b.age-a.age
//     // })
// // 按名字
// arr.sort((a,b)=>{
//     return a.name.charCodeAt()-b.name.charCodeAt()
// })
//     console.log(arr)
var arr = [
      {name:'adc', age:12},
      {name:'bgf', age:13},
      {name:'chj', age:10},
      {name:'drt', age:8},
      {name:'edf', age:22},
      {name:'fjl', age:7},
      {name:'gsn', age:65},
      {name:'hyk', age:33},
      {name:'iar', age:5},
      {name:'jbf', age:44},
      {name:'kag', age:6},
    ];

    arr.sort((a,b)=>{
        for(let i=0; i<a.name.length; i++){
            if(a.name[i]!=b.name[i]){
               return a.name[i].charCodeAt()-b.name[i].charCodeAt()
            }
        }
        return a.age-b.age
    })
    console.log(arr)
    </script>
</body>
</html>